package gui;

import business.UserManager;
import entity.Calendar;
import entity.CalendarScope;
import entity.User;
import entity.UserType;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

@Named(value = "registrationBean")
@RequestScoped
public class RegistrationBean {
    
    @EJB
    private UserManager userManager;
    
    private User user;
    
    public RegistrationBean() {
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public String register() {
        Calendar newCalendar = new Calendar();
        newCalendar.setName("default");
        newCalendar.setScope(CalendarScope.PRIVATE.toString());
        newCalendar.setOwner(user);
        
        user.setUserType(UserType.USER.toString());
        user.addCalendar(newCalendar);
        
        userManager.create(user);
        return "/user/home?faces-redirect=true";
    }
}
